import {createAsyncThunk} from "@reduxjs/toolkit";
import {DetailsOptionProps} from "../reimburseDetailReducer";
import {Result} from "type/Result";
import {requestDelete, requestGet, requestPost, requestPut} from "util/request";
import LogFactory from "log/LogFactory";
import {ReimburseDetailsType} from "type/ReimburseDetailsType";

const log = LogFactory.getLog("detailsAsyncAction") ;

interface FetchListByReimburseIdResult {
    /**
     * 根据报销单id获取报销单记录列表
     */
    data: ReimburseDetailsType[] | null; // 根据你的实际类型进行替换
    /**
     * 报销单id
     */
    reimburseId: number;
}

/**
 * 获取报销列表分页数据
 */
export const fetchListByReimburseId = createAsyncThunk(
    'reimburseDetails/fetchListByReimburseId',
    async (reimburseId: number): Promise<FetchListByReimburseIdResult> => {
        log.request("根据报销单id获取报销单记录列表", reimburseId);
        const response =
            await requestGet<ReimburseDetailsType[]>(`/record/reimburse-details/list/${reimburseId}`);
        if (response.success) {
            return {data: response.data, reimburseId};
        }
        return {data: null, reimburseId};
    }
);

/**
 * 添加查询报销单记录,添加一条报销单记录,添加成功后返回添加的报销单记录
 */
export const fetchAddDetailsQuery = createAsyncThunk(
    'reimburseDetails/fetchAddDetailsQuery',
    async (details: ReimburseDetailsType): Promise<Result<ReimburseDetailsType>> => {
        return await requestPut<ReimburseDetailsType>("/record/reimburse-details/add-query", details);
    }
);

/**
 * 更新报销记录详细信息
 */
export const fetchUpdateDetails = createAsyncThunk(
    'reimburseDetails/fetchUpdateDetails',
    async (details: ReimburseDetailsType): Promise<{ data: number, details: ReimburseDetailsType }> => {
        log.request("更新报销记录详细信息", details);
        let res = await requestPost<number>("/record/reimburse-details", details).then(res => res);
        return {
            data: res.data,
            details: details
        };
    }
);

/**
 * 删除报销记录反回结果类型
 */
interface FetchDeleteByIdResult {
    /**
     * 删除的报销记录id
     */
    id: number,
    /**
     * 删除报销记录反回的结果,如果反回1表示删除成功
     */
    data: number
}

/**
 * 根据id删除报销记录
 */
export const fetchDeleteById = createAsyncThunk(
    'reimburseDetails/fetchDeleteById',
    async (detailsId: number): Promise<FetchDeleteByIdResult> => {
        log.request("根据id删除报销记录", detailsId);
        let res = await requestDelete<number>("/record/reimburse-details/" + detailsId).then(res => res);
        return {
            id: detailsId,
            data: res.data,
        };
    }
);

/**
 * 获取报销记录可用item条目选项
 */
export const fetchGetItemOptions = createAsyncThunk(
    'reimburseDetails/fetchGetItemOptions',
    async (): Promise<Result<DetailsOptionProps[]>> => {
        log.request("获取报销记录可用item条目选项");
        return await requestGet<DetailsOptionProps[]>("/record/reimburse-details/item-options");
    }
);
/**
 * 添加报销记录条目
 * @return{Promise<{data: number, value: string}>}
 * @return{value: string} 添加的报销记录选项的值
 * @return{data: number} 添加成功结果 成功反回1
 */
export const fetchAddItemOptions = createAsyncThunk(
    'reimburseDetails/fetchAddItemOptions',
    async (option: DetailsOptionProps): Promise<{data: number, value: string}> => {
        log.request("添加报销记录可用item条目选项");
        let res = await requestPut<number>("/record/reimburse-details/item-options",option).then(res => res);
        return {
            data: res.data ,
            value: option.value
        } ;
    }
) ;
/**
 * 删除报销记录条目
 */
export const fetchDeleteItemOptions = createAsyncThunk(
    'reimburseDetails/fetchDeleteItemOptions',
    async (option: DetailsOptionProps): Promise<Result<number>> => {
        log.request("删除报销记录可用item条目选项");
        return await requestPost<number>("/record/reimburse-details/item-options",option).then(res => res);
    }
) ;
/**
 * 修改报销记录条目
 * @return {Promise<Result<number>>} 修改结果 成功反回 1
 */
export const fetchUpdateItemOptions = createAsyncThunk(
    'reimburseDetails/fetchUpdateItemOptions',
    async (option: DetailsOptionProps): Promise<Result<number>> => {
        log.request("添加报销记录可用item条目选项");
        return await requestPost<number>("/record/reimburse-details/item-options",option).then(res => res);
    }
) ;